package com.hxj.admin.szjy.service.impl;

import com.hxj.admin.szjy.constants.ProjectConstant;
import com.hxj.admin.szjy.pojo.command.ProjectSaveToNextCommand;
import com.hxj.admin.szjy.pojo.command.StartNewProjectCommand;
import com.hxj.admin.szjy.pojo.query.ProjectQuery;
import com.hxj.admin.szjy.pojo.vo.ProjectBaseInfoVO;
import com.hxj.admin.szjy.service.ProjectService;
import com.hxj.common.core.pojo.bo.AjaxResult;
import com.hxj.common.core.pojo.bo.RestResult;
import com.hxj.flowable.domain.dto.FlowTaskDto;
import com.hxj.flowable.domain.query.FlowTaskQuery;
import com.hxj.flowable.service.IFlowInstanceService;
import com.hxj.flowable.service.IFlowTaskService;
import lombok.RequiredArgsConstructor;
import org.flowable.bpmn.model.UserTask;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author hxj
 * @Date 2024/10/26 17:00
 * @Description
 */
@Component
@RequiredArgsConstructor
public class ProjectServiceImpl implements ProjectService {

    private final IFlowInstanceService flowInstanceService;
    private final IFlowTaskService flowTaskService;

    @Override
    public RestResult startNewProject(StartNewProjectCommand command) {
        Map<String, Object> variables = Collections.emptyMap();

        // TODO 交易分成不同类型时 可能是走不同流程 这里会分出多个procDefId
        AjaxResult ajaxResult = flowInstanceService.startProcessInstanceByKey(ProjectConstant.PROC_DEF_KEY, command.getProjectNo(), variables);
        return getRestResultByAjaxResult(ajaxResult);
    }

    @Override
    public RestResult saveAndCompleteToNext(ProjectSaveToNextCommand command) {
        // saveFormData();// 保存表单数据
        return completeToNext(command);
    }

    @Override
    public RestResult completeToNext(ProjectSaveToNextCommand command) {
        String projectNo = command.getProjectNo();
        Optional<FlowTaskDto> flowTaskDto = flowTaskService.queryTaskInfo(new FlowTaskQuery(projectNo));
        if (!flowTaskDto.isPresent()) {
            return RestResult.fail("查无数据！");
        }
        FlowTaskDto taskInfo = flowTaskDto.get();
        command.setTaskId(taskInfo.getTaskId());

        // TODO 判断是否处在同一流程 避免重复提交
        // TODO 加锁防止并发问题？锁业务key？
        // TODO 判断是否允许提交到下一步？是否有拦截条件不满足？taskReject?
        AjaxResult complete = flowTaskService.complete(command);
        return getRestResultByAjaxResult(complete);
    }

    @Override
    public RestResult returnToLast(ProjectSaveToNextCommand command) throws Exception {
        Optional<FlowTaskDto> flowTaskDto = flowTaskService.queryTaskInfo(new FlowTaskQuery(command.getProjectNo()));
        if (!flowTaskDto.isPresent()) {
            return RestResult.fail("查无数据！");
        }
        FlowTaskDto taskInfo = flowTaskDto.get();
        command.setTaskId(taskInfo.getTaskId());

        List<UserTask> userTaskList = flowTaskService.findReturnTaskList(command);
        if (userTaskList.size() == 0) {
            return RestResult.fail("找不到上一个节点！");
        }
        command.setTargetKey(userTaskList.get(0).getId());

        // TODO 判断是否处在同一流程 避免重复提交
        // TODO 加锁防止并发问题？锁业务key？
        // TODO 判断是否允许提交到下一步？是否有拦截条件不满足？taskReject?
        flowTaskService.taskReturn(command);
        return RestResult.success();
    }

    @Override
    public Optional<FlowTaskDto> getProjectBaseInfo(ProjectQuery query) {
        FlowTaskQuery flowTaskQuery = new FlowTaskQuery(query.getProjectNo());
        return flowTaskService.queryTaskInfo(flowTaskQuery);
    }

    // 临时方法
    private static RestResult getRestResultByAjaxResult(AjaxResult ajaxResult) {
        if (ajaxResult.isSuccess()) {
            return RestResult.success(ajaxResult.get(AjaxResult.DATA_TAG));
        }
        return RestResult.fail((String) ajaxResult.get(AjaxResult.MSG_TAG));
    }

}
